package com.ash.interceptor;

import com.ash.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/3/30 21:26
 * @file : project06.com.ash.interceptor.LoginInterceptor.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */

public class LoginInterceptor implements HandlerInterceptor {
    /*登录拦截*/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        System.out.println("登录拦截===============================================");
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if(user!=null){
            return true;
        }
        else {
            /*如果没有user的话就会直接跳转到登录页面*/
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("拦截后");
    }
}
